from threading import Thread,Lock

num = 1
lock = Lock()
def work1():
    global num
    lock.acquire() # 上锁
    for i in range(100000):
        num += i

    lock.release() # 释放锁
    print('work1输出num:',num)


def work2():
    global num
    lock.acquire()
    for i in range(100000):
        num += i
    lock.release()
    print('work2输出num:',num)

if __name__ == "__main__":
    print('父进程开始运行')
    t1 = Thread(target=work1)
    t2 = Thread(target=work2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print('主线程的num:',num)